package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.OutcomeManage;
import cn.wolfcode.crm.query.OutcomeManageQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IOutcomeManageService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("outcomeManage")
public class OutcomeManageController {
    @Autowired
    private IOutcomeManageService outcomeManageService;

    @RequiresPermissions(value = {"outcomeManage:view", "支出查看"}, logical = Logical.OR)
    @RequestMapping("view")
    public String view() {
        return "outcomeManage/outcomeManage";
    }

    @RequiresPermissions(value = {"outcomeManage:query", "支出列表"}, logical = Logical.OR)
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(OutcomeManageQueryObject qo) {
        return outcomeManageService.quey4List(qo);
    }


    @RequiresPermissions(value = {"outcomeManage:saveOrUpdate", "支出新增/编辑"}, logical = Logical.OR)
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(OutcomeManage entity) {
        try {
            outcomeManageService.saveOrUpdate(entity);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败!原因:" + e.getMessage());
        }
    }

    @RequestMapping("exportXls")
    public void exportXls(HttpServletResponse response, OutcomeManageQueryObject qo) throws Exception {
        //设置文件下载响应头

        response.setHeader("Content-Disposition", "attachment;filename=outcomeManage.xls");

        Workbook book = outcomeManageService.exportXls(qo);

        //写入数据输出到浏览器
        book.write(response.getOutputStream());
    }

    @RequiresPermissions(value = {"outcomeManage:delete", "支出删除"}, logical = Logical.OR)
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult result = new JsonResult();
        try {
            outcomeManageService.delete(id);

        } catch (Exception e) {
            e.printStackTrace();
            return result.mark("操作失败!原因:" + e.getMessage());
        }
        return result;
    }

    @RequiresPermissions(value = {"outcomeManage:delete", "支出审核"}, logical = Logical.OR)
    @RequestMapping("auditor")
    @ResponseBody
    public JsonResult auditor(Long id) {
        JsonResult result = new JsonResult();
        try {
            outcomeManageService.auditor(id);

        } catch (Exception e) {
            e.printStackTrace();
            return result.mark("操作失败!原因:" + e.getMessage());
        }
        return result;
    }
}
